const Utils = require('../utils');

module.exports = (options = {}) => {
  const {
    match,
    ignore,
  } = options;
  return async (req, res, next) => {
    const reqPath = req.path;
    if (
      (match && !match.test(reqPath)) ||
      (ignore && ignore.test(reqPath))
    ) {
      return next();
    }
    const _json = res.json;
    res.json = (...args) => {
      Utils.Logger.info(`
      url: ${reqPath}
      method: ${req.method}
      入参数据: ${JSON.stringify(req.method === 'GET' ? req.query : req.body)}
      返参数据: ${typeof args[0] === 'object' ? JSON.stringify(args[0]) : args.toString()}
      `);
      return _json.apply(res, args);
    }
    next();
  }
}